const db = require('./server/config/database')

async function testFixedQuery () {
  try {
    console.log('测试修复后的查询...')

    // 测试LIMIT和OFFSET的字符串拼接方式
    const limit = 5
    const offset = 0

    const query = `SELECT * FROM novels WHERE 1=1 ORDER BY updated_at DESC LIMIT ${limit} OFFSET ${offset}`
    console.log('SQL:', query)

    const novels = await db.query(query, [])
    console.log(`✅ 查询成功: ${novels.length} 条记录`)

    // 处理数据格式
    const formattedNovels = novels.map(novel => ({
      ...novel,
      tags: novel.tags || []
    }))

    console.log('前2本小说:')
    formattedNovels.slice(0, 2).forEach(novel => {
      console.log(`- ${novel.title} (${novel.author})`)
      console.log(
        `  状态: ${novel.status}, 标签: ${JSON.stringify(novel.tags)}`
      )
    })

    const result = {
      success: true,
      data: formattedNovels,
      total: formattedNovels.length
    }

    console.log('\n✅ 修复后的查询测试成功！')
  } catch (error) {
    console.error('❌ 查询测试失败:', error)
  } finally {
    process.exit(0)
  }
}

testFixedQuery()
